package sifcom.Utilidades;

import java.io.BufferedReader;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.repackaged.org.json.JSONObject;

public class Utilidades {
	
	public JSONObject getJsonObject(BufferedReader reader){
		try{
	    StringBuilder sb = new StringBuilder();
	    String line = reader.readLine();
	    while (line != null) {
	        sb.append(line + "\n");
	        line = reader.readLine();
	    }
	    reader.close();
	    String data = sb.toString();	

		String datos = data.substring(8, data.length()-2);
		JSONObject obj = new JSONObject(datos);
		return obj;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}
	
	public Entity getEntity(String id)
	{
		Key socioKey = KeyFactory.stringToKey(id);				
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		try {
			Entity entidad = datastore.get(socioKey);
			return entidad;			
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
}
